<?php
namespace app\api\controller;

use app\api\model\ExaminelogModel;
use app\api\model\GoodsgroupModel;
use app\api\model\OrderModel;
use app\api\model\UserModel;
use app\api\model\Myself as MyselfModel;
use app\api\model\BankModel as Bank;
use app\api\model\QrcodeModel as Qrcode;
use app\api\model\Withdrawal as Withdrawal;
use app\api\model\BalancelogModel as Balance;
use http\Params;

class User extends Base
{
    public $user;
    public $myself;
    public $bank;
    public $qrcode;
    public $withdraw;
    public $balance;
    public function __construct()
    {
        parent::__construct();
        $this->user = new UserModel;
        $this->myself = new MyselfModel();
        $this->bank = new Bank();
        $this->qrcode = new Qrcode();
        $this->withdraw = new Withdrawal();
        $this->balance = new Balance();
    }

    public function index(){
    }
    //获取用户信息
    public function details()
    {
        $uid=$this->uid;
        $rs=$this->myself->myself($uid);
        if ($rs)
        {
            $this->success("成功",$rs,"1");
        }else{
            $this->error("获取用户信息失败",'','401');
        }
    }
    //个人资料修改
    public function personal_data_edit(){
        if($this->request->isPost()){
            $uid = $this->uid;
            $data = input();
            $qq_num = $data['qq_num'];
            $wechart_num = $data['wechart_num'];
            $ali_num = $data['ali_num'];
            $data = array();
            $data ['u_qq'] = $qq_num;
            $data ['wx_no'] = $wechart_num;
            $data ['alipay'] = $ali_num;
            $data ['u_ztnum'] = 1;
            $res = $this->user->where("uid",$uid)->update($data);
            if($res){
                $this->success("修改成功",'','1');
            }else{
                $this->error("修改失败",'','-1');
            }
        } else {
            $this->error("提交信息错误",'','-1');
        }
    }
    //修改密码
    public function forget_password(){
        if($this->request->isPost()){
            $oldpassword = input("oldpassword");
            $newpassword = input("newpassword");
            $user_info = $this->user->where("uid",$this->uid)->find();
            if($user_info){
                if(md5(md5($oldpassword)) == $user_info['u_pwd']){
                    $res = $this->user->where("uid",$this->uid)->update(array("u_pwd"=>md5(md5($newpassword))));
                    if($res){
                        $this->success("修改成功",'','1');
                    }else{
                        $this->error("修改失败",'','-1');
                    }
                }else{
                    $this->error("旧密码输入有误",'','-1');
                }
            }else{
                $this->error("没有该用户",'','-1');
            }
        }else{
            $this->error("提交信息错误",'','-1');
        }
    }
    //银行卡列表
    public function my_bank(){
        $uid = $this->uid;
        $list = $this->bank->where("uid",$uid)->where("b_state",1)->select();
        if(!empty($list)){
            $this->success("",$list,"1");
        }else{
            $this->error("您还没有添加银行卡",'','-1');
        }
    }
    //添加银行卡
    public function add_bank(){
        if($this->request->isPost()){
            $uid = $this->uid;
            $b_card = input("card_num");
            $b_username = input("bank_username");
            $b_bank = input("bank_name");
            if(empty($b_card) || empty($b_username) || empty($b_bank)){
                $this->error("信息传输错误",'','-1');
            }
            $arr = array();
            $arr ['b_card'] = $b_card;
            $arr ['b_username'] = $b_username;
            $arr ['b_bank'] = $b_bank;
            $arr ['uid'] = $uid;
            $res = $this->bank->insert($arr);
            if($res){
                $this->success("添加成功",'','1');
            }else{
                $this->error("添加失败",'','-1');
            }
        }else{
            $this->error("提交信息错误",'','-1');
        }
    }
    //删除银行卡
    public function bank_del(){
        if($this->request->isPost()){
            $uid = $this->uid;
            $bank_id = input("bank_id");
            if(empty($bank_id)){
                $this->error("信息传输错误",'','-1');
            }
            $bank_info = $this->bank->where("uid",$uid)->where("bid",$bank_id)->find();
            if(empty($bank_info)){
                $this->error("您没有该银行卡",'','-1');
            }
            $res = $this->bank->where("bid",$bank_id)->update(array("b_state"=>0));
            if($res){
                $this->success("删除成功",'','1');
            }else{
                $this->error("删除失败",'','-1');
            }
        }else{
            $this->error("提交信息错误",'','-1');
        }
    }
    //二维码列表
    public function qrcode_list(){
        if($this->request->isPost()){
            $uid = $this->uid;
            $type_id = input("type_id");
            if(empty($type_id)){
                $this->error("信息传输错误",'','-1');
            }
            $qrcodelist = $this->qrcode->where("uid",$uid)->where("q_state",1)->where("zid",$type_id)->select();
            if(empty($qrcodelist)){
                $this->error("您没有二维码",'','-1');
            }else{
                $this->success("成功",$qrcodelist,"1");
            }
        }else{
            $this->error("提交信息错误",'','-1');
        }
    }
    //二维码详情
    public function qrcode_detail(){
        if($this->request->isPost()){
            $uid = $this->uid;
            $qid = input("qid");
            if(empty($qid)){
                $this->error("信息传输错误",'','-1');
            }
            $qrcodelist = $this->qrcode->where("uid",$uid)->where("q_state",1)->where("qid",$qid)->find();
            if(empty($qrcodelist)){
                $this->error("您没有二维码",'','-1');
            }else{
                $this->success("成功",$qrcodelist,"1");
            }
        }else{
            $this->error("提交信息错误",'','-1');
        }
    }
    //添加二维码
    public function qrcode_add(){
        if($this->request->isPost()){
            $uid = $this->uid;
            $type_id = input("type_id");
            $q_picture = input("picture");
            $num = input("num");
            $name = input("name");
            if(empty($type_id) || (empty($q_picture)) || (empty($num)) || (empty($name))){
                $this->error("信息传输错误",'','-1');
            }
            $arr = array();
            $arr ['uid'] = $uid;
            $arr ['zid'] = $type_id;
            $arr ['q_picture'] = $q_picture;
            $arr ['q_createtime'] = date("Y-m-d H:i:s");
            $arr ['q_num'] = $num;
            $arr ['q_name'] = $name;
            $res = $this->qrcode->insert($arr);
            if($res){
                $this->success("添加成功",'',"1");
            }else{
                $this->error("添加错误",'','-1');
            }
        }else{
            $this->error("提交信息错误",'','-1');
        }
    }
    //上传二维码
    public function qrcode_upload(){
        $file = request()->file('file');
        if(!empty($file)){
            $uid = $this->uid;
            $fileName = time().$uid;
            $fileRoute = 'uploads/users/qrcode/';
            $small_pics = $file->move($fileRoute, $fileName);
            if($small_pics){
                $backfile = '/'.$fileRoute.$small_pics->getFilename();
                $arr = array();
                $arr['img'] = $backfile;
                $this->success("上传成功",$arr,"1");
            }else{
                $this->error("上传失败",'',"-1");
            }
            // $data['head']= str_replace(' ','+',$data['head']);
            //  $tagUrl = $this->base64_image_content($data['head'],$uploadUrl);
        }else{
            $this->error("请选择一张图片",'',"-1");
        }
    }
    //我要邀请
    public function my_yqm(){
        $uid = $this->uid;
        $user_info = $this->user->where("uid",$uid)->find();
        $arr = array();
        $arr['url'] = "http://api.mamingwang.cn/index.php/index/register/register?yqm=".$user_info['yqm'];
        $arr['yqm'] = $user_info['yqm'];
        $this->success("成功",$arr,'1');
    }
    //我要提现
    public function withdraw(){
        if($this->request->isPost()){
            $uid = $this->uid;
            $type_id = input("type_id");
            $num = abs(input("num"));
            $bank_id = 0;
            $qid = 0;
            $userinfo = $this->user->where("uid",$this->uid)->find();
            if($userinfo['y_money'] < $num){
                $this->error("您的佣金不足",'','-1');
            }
            if($type_id == 3){
                $bank_id = input("bank_id");
            }elseif($type_id == 2){
                $qid = $this->qrcode->where("uid",$this->uid)->where("zid",2)->where("q_state",1)->find();
            }elseif($type_id == 1){
                $qid = $this->qrcode->where("uid",$this->uid)->where("zid",1)->where("q_state",1)->find();
            }
            if(empty($type_id) || (empty($num))){
                $this->error("信息传输错误",'','-1');
            }
            $this->withdraw->startTrans();
            $arr = array();
            $arr ['w_number']=date('YmdHis').$uid.rand(0,100);
            $arr ['uid'] = $uid;
            $arr ['w_money'] = $num;
            $arr ['w_createtime'] = date("Y-m-d H:i:s");
            $arr ['qid'] = $qid['qid'];
            $arr ['bid'] = $bank_id;
            $res = $this->withdraw->insert($arr);
            $res1 = $this->user->where("uid",$this->uid)->setDec("y_money",$num);
            if($res && $res1){
                $this->withdraw->commit();
                $this->success("申请成功",'',"1");
            }else{
                $this->withdraw->rollback();
                $this->error("申请错误",'','-1');
            }
        }else{
            $this->error("提交信息错误",'','-1');
        }
    }
    //资金明细
    public function balancelog(){
        $uid = $this->uid;
        $list = $this->balance->where("uid",$uid)->order("id desc")->select();
        if(!empty($list)){
            $this->success("成功",$list,"1");
        }else{
            $this->error("暂时没有信息",'','-1');
        }
    }
}